package com.zhanghe.util;

import java.io.File;

import com.zhanghe.mapper.ImageFileMapper;
import com.zhanghe.model.ImageFile;

public class FileUtil {
	public static void main(String[] args) {
		traverseFolder2("H:\\");
	}

	public static void traverseFolder2(String path) {

		File file = new File(path);
		if (file.exists()) {
			File[] files = file.listFiles();
			if (files != null) {
				if (files.length == 0) {
					System.out.println("文件夹是空的!");
					return;
				} else {
					for (File file2 : files) {
						if (file2.isDirectory()) {
							System.out.println("文件夹:" + file2.getAbsolutePath());
							traverseFolder2(file2.getAbsolutePath());
						} else {
							System.out.println("文件:" + file2.getAbsolutePath());
							System.out.println("文件所在文件夹:"+file2.getParentFile().getName());
							System.out.println("文件parent:"+path);
						}
					}
				}
			}
		} else {
			System.out.println("文件不存在!");
		}
	}
	public static void traverseFolder2(String path,ImageFileMapper mapper) {

		File file = new File(path);
		if (file.exists()) {
			File[] files = file.listFiles();
			if (files != null) {
				if (files.length == 0) {
					System.out.println("文件夹是空的!");
					return;
				} else {
					for (File file2 : files) {
						if (file2.isDirectory()) {
							System.out.println("文件夹:" + file2.getAbsolutePath());
							traverseFolder2(file2.getAbsolutePath(),mapper);
						} else {
							ImageFile imagefile=new ImageFile();
							imagefile.setFileName(file2.getName());
							imagefile.setFilePath(file2.getAbsolutePath());
							imagefile.setFolder(file2.getParentFile().getName());
							imagefile.setFiletype(1);
							mapper.insert(imagefile);
							System.out.println("文件:" + file2.getAbsolutePath());
							System.out.println("文件所在文件夹:"+file2.getParentFile().getName());
							System.out.println("文件parent:"+path);
						}
					}
				}
			}
		} else {
			System.out.println("文件不存在!");
		}
	}
}
